// led_manager.h
#ifndef LED_MANAGER_H
#define LED_MANAGER_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#define MAX_LEDS 16
#define MAX_LOCKED 16

typedef enum {
	// 常闭模式
	LED_OFF,
	// 常亮模式
	LED_ON,
	// 闪烁模式
	LED_BLINK,
	// 跑马灯模式
	LED_FLOW,
	LED_BREATH,
	// PWM 模式
	LED_BREATH_PWM,
	// change模式
	LED_CHANGE
} LedMode;

/**
 *  // 启动
 *  startLedManagerTask(20);
	// 加入LED引脚（必须先加入）
 *  addLedWithInit(GPIO_NUM_32);
	// 配置LED
 *  LedConfig led_config = {.interval = 200,
							.mode = LED_BLINK,
							.pin = GPIO_NUM_32,
							.pwm_channel = LEDC_CHANNEL_2};

	// 设置模式
	led_manager_set_mode(led_config);
 */

typedef struct {
	uint8_t pin;
	LedMode mode;
	unsigned long interval;
	uint8_t pwm_channel;
	bool privileged;
} LedConfig;

typedef void (*DigitalWriteFn)(uint8_t pin, bool level);
typedef uint32_t (*MillisFn)(void);
typedef void (*PwmSetupFn)(uint8_t pin, uint8_t channel);
typedef void (*PwmWriteFn)(uint8_t channel, uint8_t duty);
typedef void (*PwmDetachFn)(uint8_t pin);

typedef struct {
	uint8_t pin;
	LedMode mode;
	unsigned long interval;
	unsigned long lastToggle;
	bool isOn;
	uint8_t brightness;
	uint8_t pwmChannel;
	bool breathingUp;
} LedState;
#ifdef __cplusplus
extern "C" {
#endif
void led_manager_init(DigitalWriteFn write, MillisFn millis, PwmSetupFn setup,
					  PwmWriteFn writePwm, PwmDetachFn detach);

void led_manager_add(uint8_t pin);
void led_manager_remove(uint8_t pin);
void led_manager_set_mode(LedConfig config);
LedMode led_manager_get_mode(uint8_t pin);

void led_manager_set_flow_interval(unsigned long interval);
void led_manager_set_flow_direction(bool forward);
void led_manager_lock_pins(const uint8_t *pins, size_t count);
void led_manager_unlock_pins(const uint8_t *pins, size_t count);
bool led_manager_is_locked(uint8_t pin);
void led_manager_change(uint8_t pin);
void led_manager_update(void);
LedState *led_manager_get_all(void);
int led_manager_get_count(void);
void led_manager_clean_all_mode(void);
#ifdef __cplusplus
}
#endif
#endif // LED_MANAGER_H
